<?php
    // +----------------------------------------------------------------------
    // | Created by PHPstorm: JRKAdmin框架 [ JRKAdmin ] 
    // +----------------------------------------------------------------------
    // | Copyright (c) 2019~2022 [LuckyHHY] All rights reserved.
    // +----------------------------------------------------------------------
    // | SiteUrl: http://www.luckyhhy.cn
    // +----------------------------------------------------------------------
    // | Author: LuckyHhy <jackhhy520@qq.com>
    // +----------------------------------------------------------------------
    // | Date: 2020/3/4-16:47
    // +----------------------------------------------------------------------
    // | Description:  
    // +----------------------------------------------------------------------


    namespace app\admin\controller;


    use Jrk\Excel;

    class Friendlink extends AdminBase
    {

        public function initialize()
        {
            parent::initialize(); // TODO: Change the autogenerated stub

            $this->model=app()->model("Friendinks");

        }


        /**
         * @return mixed
         * @throws \think\Exception
         * @throws \think\exception\PDOException
         * @author: LuckyHhy <jackhhy520@qq.com>
         * @date: 2020/3/12 0012
         * @name: addFriend
         * @describe:
         */
        public function addFriend(){
            if (IS_POST){
                $data = $this->request->post();

                if (isset($data['id'])){
                    addLog("友情链接修改操作","修改友情链接：".$data['name']);
                }else{
                    addLog("友情链接添加操作","新增友情链接：".$data['name']);
                }
                return $this->model->addAndEdit($data);
            }
            return $this->fetch();
        }



        /**
         * @return mixed
         * @author: LuckyHhy <jackhhy520@qq.com>
         * @date: 2020/3/4
         * @name: editFriend
         * @describe:编辑
         */
        public function editFriend(){

            $id   = $this->request->param("id/d");//父id

            $info = $this->model->getOne(['id'=>$id]);

            if (empty($info)){
                $this->assign('msg','当前id:'.$id.'错误');
                $this->fetch("public/404");
            }
            $this->assign(compact( "info"));
            return $this->fetch();
        }


        /**
         * @throws \PHPExcel_Reader_Exception
         * @throws \PHPExcel_Writer_Exception
         * @author: LuckyHhy <jackhhy520@qq.com>
         * @date: 2020/3/17 0017
         * @name: export
         * @describe:
         */
        public function export(){
            $ids=$this->request->param("id");
            if ($ids=="all"){
                $list=$this->model->order("create_time desc")->select();
            }else{
                $list=$this->model->where('id',"in",$ids)->order("create_time desc")->select();
            }
            if (empty($list)){
                $this->redirect(url("friendlink/index"));exit;
            }
            $arr=[];
            foreach ($list as $v){
                $arr[]=[
                    $v['name'],$v['href'],$v['email'],$v['desc']
                ];
            }
            Excel::setExcelHeader(['链接名称','地址','邮箱','描述'])
                ->setExcelTile('友情链接列表数据', '链接数据',date('Y-m-d H:i:s',time()))
                ->setExcelContent($arr)
                ->ExcelSave();
        }

    }